package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.ShoppingMallApplication;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.business.ProductCollectionBusiness;
import com.showde.shoppingmall.db.ProductCollectionDBCommand;
import com.showde.shoppingmall.domain.ProductCollection;
import com.showde.shoppingmall.netentity.ProductCollectionEntity.CollectionOperatorType;

public class ProductCollectionLogic {

	public static final void deleteCollectedProduct(final Context context,
			final String productId,
			final UICallbackInterface uiCallbackInterface) {

		ProductCollectionBusiness productCollectionBusiness = new ProductCollectionBusiness(
				productId, new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						if (jsonObject == null) {
							uiCallbackInterface.onError("no response data...");
							return;
						}
						try {
							String returnCode = jsonObject
									.getString("ReturnCode");
							if ("0".equals(returnCode)) {
								// 成功成功
								ProductCollectionDBCommand collectionDBCommand = new ProductCollectionDBCommand(
										context);
								collectionDBCommand.delete(productId);
								uiCallbackInterface.onSuccess(null);
								return;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						uiCallbackInterface
								.onError("parse response data error ...");
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				}, CollectionOperatorType.delete);
		productCollectionBusiness.doBusiness();

	}

	/**
	 * 添加收藏
	 * 
	 * @param context
	 * @param productId
	 * @param uiCallbackInterface
	 */
	public static final void collectionProduct(final Context context,
			final String productId,
			final UICallbackInterface uiCallbackInterface) {

		if (ShoppingMallApplication.userSession == null
				|| ShoppingMallApplication.userSession.getSessionUser() == null
				|| ShoppingMallApplication.userSession.getSessionUser()
						.getUserAccount() == null) {
			uiCallbackInterface.onError("no account");
			return;
		}

		ProductCollectionBusiness productCollectionBusiness = new ProductCollectionBusiness(
				productId, new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						if (jsonObject == null) {
							uiCallbackInterface.onError("no response data...");
							return;
						}
						try {
							String returnCode = jsonObject
									.getString("ReturnCode");
							if ("0".equals(returnCode)) {
								// 收藏成功
								ProductCollectionDBCommand collectionDBCommand = new ProductCollectionDBCommand(
										context);
								ProductCollection t = new ProductCollection();
								t.setProductId(productId);
								collectionDBCommand.add(t);
								uiCallbackInterface
										.onSuccess(new CallbackData());
								return;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						uiCallbackInterface
								.onError("parse response data error ...");
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				}, CollectionOperatorType.add);
		productCollectionBusiness.doBusiness();
	}

	/**
	 * 查询收藏
	 * 
	 * @param context
	 * @param uiCallbackInterface
	 */
	public static final void getCollectionProduct(final Context context,
			final UICallbackInterface uiCallbackInterface) {
		if (ShoppingMallApplication.userSession == null
				|| ShoppingMallApplication.userSession.getSessionUser() == null
				|| ShoppingMallApplication.userSession.getSessionUser()
						.getUserAccount() == null) {
			uiCallbackInterface.onError("no account");
			return;
		}

		ProductCollectionBusiness productCollectionBusiness = new ProductCollectionBusiness(
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						JSONObject jsonObject = netEntity.result;
						if (jsonObject == null) {
							uiCallbackInterface.onError("no response data.");
							return;
						}
						try {
							String returnCode = jsonObject
									.getString("ReturnCode");
							if ("0".equals(returnCode)) {
								JSONArray array = jsonObject
										.getJSONArray("list");
								if (array != null && array.length() > 0) {
									List<ProductCollection> productCollections = new ArrayList<ProductCollection>();
									ProductCollection collection = null;
									for (int index = 0; index < array.length(); index++) {
										collection = new ProductCollection();
										JSONObject arrayItem = array
												.getJSONObject(index);
										collection.setProductId(arrayItem
												.getString("goodsid"));
										collection.setCollectiontime(arrayItem
												.getString("addtime"));
										productCollections.add(collection);
									}
									ProductCollectionDBCommand collectionDBCommand = new ProductCollectionDBCommand(
											context);
									collectionDBCommand.add(productCollections);
									uiCallbackInterface
											.onSuccess(new CallbackData());
									return;
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						uiCallbackInterface.onError("parse error.");
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						uiCallbackInterface.onError(errorMsg);
					}
				});
		productCollectionBusiness.doBusiness();
	}
}
